//
//  CBGHomeViewController.m
//  chebabaGYS
//
//  Created by zhengminjie on 15/12/24.
//  Copyright © 2015年 zhengminjie. All rights reserved.
//

#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>

#import "CBGRepairingViewController.h"
#import "CBGHomeViewController.h"
#import "CBGAllorderData.h"

#import "CustomDefine.h"

@interface CBGHomeViewController ()<UITableViewDataSource,UITableViewDelegate,BMKLocationServiceDelegate>{
    BMKLocationService *_locationService;

    double _latitudeUser;
    double _longtitudeUser;
}

@property (strong, nonatomic) IBOutlet UIImageView *xiaLaImageView;
@property (strong, nonatomic) IBOutlet UILabel *xiaLaLb;
@property (strong, nonatomic) IBOutlet UIButton *xiaLaBtn;
@property (strong, nonatomic) IBOutlet UIView *saiXuanView;
@property (strong, nonatomic) IBOutlet UIView *zheDangView;
@property (strong, nonatomic) IBOutlet UILabel *quanbuLb;
@property (strong, nonatomic) IBOutlet UILabel *qiangxiuLb;
@property (strong, nonatomic) IBOutlet UILabel *yuYueLb;




@property (assign, nonatomic) BOOL isShaiXuan;

@property (strong, nonatomic) IBOutlet UIView *mainView;

@property (strong, nonatomic) UITableView *tabelView;

@property (strong, nonatomic) NSArray *allOrderArray;

@property (strong, nonatomic) IBOutlet UIImageView *xiabanIV;
@property (strong, nonatomic) IBOutlet UIImageView *zhantingIV;
@property (strong, nonatomic) IBOutlet UIImageView *shangbanIV;
@property (strong, nonatomic) IBOutlet UIImageView *qidongIV;
@property (strong, nonatomic) IBOutlet UIButton *xiabanBtn;
@property (strong, nonatomic) IBOutlet UIButton *zhantingBtn;
@property (strong, nonatomic) IBOutlet UIButton *shangbanBtn;
@property (strong, nonatomic) IBOutlet UIButton *qidongBtn;


@property (strong, nonatomic) UIView *tishiView;
@property (strong, nonatomic) UIView *tishiBGView;

@property (assign, nonatomic) int indexSection;

@end

@implementation CBGHomeViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    _locationService.delegate = self;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    _locationService.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //隐藏筛选视图
    [self hiddenShaiXuanView];
    
    //创建tabelview
    [self createTabelView];
    
    //请求订单数据
    [self requsetAllOrderMsg];
    
    //定位获取经纬度
    [self getUserLocation];
    
}

- (void)getUserLocation{
    
    _locationService = [[BMKLocationService alloc]init];
    _locationService.delegate = self;
    [_locationService startUserLocationService];
}

//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    _latitudeUser = userLocation.location.coordinate.latitude;
    _longtitudeUser = userLocation.location.coordinate.longitude;
    //NSLog(@"纬度:%f 经度:%f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
}

- (void)requsetAllOrderMsg{
    
    NSDictionary *dic = @{@"wei":[NSString stringWithFormat:@"%f",_latitudeUser],@"jing":[NSString stringWithFormat:@"%f",_longtitudeUser]};
    self.allOrderArray = [CBGAllorderData analysisAllOrderBy];
//    [CBGRequsetServers showAllOrderByParameters:dic andSuccess:^(id sender) {
//        
//    } andFailure:^(id sender) {
//        
//    }];
}

- (void)createTabelView{
    
    _tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, __kWidth, __kHeight) style:UITableViewStyleGrouped];
    _tabelView.delegate = self;
    _tabelView.dataSource = self;
    _tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tabelView.backgroundColor = [UIColor whiteColor];
    [self.mainView addSubview:_tabelView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.allOrderArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        CBGAllorderData *allOrder = self.allOrderArray[indexPath.section];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.05];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, __kWidth-50, 132)];
        view.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:view];
        
        UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10, 28, 28)];
        imageview.image = [UIImage imageNamed:@"chizi01.png"];
        [view addSubview:imageview];
        UIImageView *imageview1 = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10+28+6+8, 28, 28)];
        imageview1.image = [UIImage imageNamed:@"dezhi.png"];
        [view addSubview:imageview1];
        UIImageView *imageview2 = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10+28+6+8+28+8+6, 28, 28)];
        imageview2.image = [UIImage imageNamed:@"weixiu.png"];
        [view addSubview:imageview2];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15+28+10, 10, __kWidth-15-28-10-55, 14)];
        label.font = MFont(12);
        label.textColor = [UIColor darkGrayColor];
        label.text = allOrder.carname;
        [view addSubview:label];
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(15+28+10, 10+14, __kWidth-15-28-10-55, 14)];
        label1.font = MFont(12);
        label1.textColor = [UIColor darkGrayColor];
        label1.text = allOrder.carname;
        [view addSubview:label1];
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(15+28+10, 10+14+14+20, __kWidth-15-28-10-55, 14)];
        label2.font = MFont(12);
        label2.textColor = [UIColor darkGrayColor];
        label2.text = allOrder.carname;
        [view addSubview:label2];
        UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(15+28+10, 10+14+14+20+42, __kWidth-15-28-10-55, 14)];
        label3.font = MFont(12);
        label3.textColor = [UIColor darkGrayColor];
        label3.text = allOrder.carname;
        [view addSubview:label3];
        
        UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(__kWidth-50, 0, 50, 56)];
        label4.font = MFont(12);
        label4.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
        label4.text = @"抢修单";
        label4.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        label4.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:label4];
        
        UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(__kWidth-50, 56, 50, 132-56)];
        label5.font = MFont(12);
        label5.backgroundColor = [UIColor orangeColor];
        label5.text = @"抢单";
        label5.textColor = [UIColor whiteColor];
        label5.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:label5];
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(__kWidth-50, 56, 50, 132-56)];
        [btn setBackgroundImage:[UIImage imageNamed:@"btnBG.png"] forState:UIControlStateNormal];
        //[view addSubview:btn];
        
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 132;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    self.indexSection = indexPath.section;
    self.tishiBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, __kWidth, __kHeight)];
    self.tishiBGView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [self.view.window addSubview:self.tishiBGView];
    self.tishiView = [[UIView alloc] initWithFrame:CGRectMake(__kWidth/2-120, __kHeight/2-70, 240, 140)];
    self.tishiView.backgroundColor = [UIColor whiteColor];
    [self.tishiBGView addSubview:self.tishiView];
    
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 240, 99)];
    lab.text = @"点击确定接单啦！";
    lab.font = MFont(15);
    lab.textColor = [UIColor darkGrayColor];
    lab.textAlignment = NSTextAlignmentCenter;
    [self.tishiView addSubview:lab];
    
    UIView *ge = [[UIView alloc] initWithFrame:CGRectMake(0, 99, 240, 1)];
    ge.backgroundColor = [UIColor lightGrayColor];
    [self.tishiView addSubview:ge];
    UIView *ge1 = [[UIView alloc] initWithFrame:CGRectMake(120-1, 100, 1, 40)];
    ge1.backgroundColor = [UIColor lightGrayColor];
    [self.tishiView addSubview:ge1];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 119, 40)];
    btn.tag = 1;
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    btn.titleLabel.font = MFont(15);
    [btn addTarget:self action:@selector(sureGetOrder:) forControlEvents:UIControlEventTouchUpInside];
    [self.tishiView addSubview:btn];
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(120, 100, 120, 40)];
    btn1.tag = 2;
    [btn1 setTitle:@"取消" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    btn1.titleLabel.font = MFont(13);
    [btn1 addTarget:self action:@selector(sureGetOrder:) forControlEvents:UIControlEventTouchUpInside];
    [self.tishiView addSubview:btn1];
    
}

- (void)sureGetOrder:(UIButton *)sender{
    [self.tishiBGView removeFromSuperview];
    NSLog(@"%d",self.indexSection);
    if (sender.tag == 1) {
        
        CBGRepairingViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"CBGRepairingViewController"];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        
    }
}

- (IBAction)userWorkingTimeChose:(UIButton *)sender {
    
    switch (sender.tag) {
        case 0:
            self.xiabanIV.image = ZMJImage(@"Work2.png");
            self.zhantingIV.image = ZMJImage(@"pause.png");
            self.shangbanIV.image = ZMJImage(@"Work.png");
            self.qidongIV.image = ZMJImage(@"start.png");
            break;
        case 1:
            self.xiabanIV.image = ZMJImage(@"Work-05.png");
            self.zhantingIV.image = ZMJImage(@"pause2.png");
            self.shangbanIV.image = ZMJImage(@"Work.png");
            self.qidongIV.image = ZMJImage(@"start.png");
            break;
        case 2:
            self.xiabanIV.image = ZMJImage(@"Work-05.png");
            self.zhantingIV.image = ZMJImage(@"pause.png");
            self.shangbanIV.image = ZMJImage(@"Work2-07.png");
            self.qidongIV.image = ZMJImage(@"start.png");
            break;
        case 3:
            self.xiabanIV.image = ZMJImage(@"Work-05.png");
            self.zhantingIV.image = ZMJImage(@"pause.png");
            self.shangbanIV.image = ZMJImage(@"Work.png");
            self.qidongIV.image = ZMJImage(@"start2.png");
            break;
    }
    
    
    
    
}





//下拉菜单逻辑
- (void)hiddenShaiXuanView{
    self.isShaiXuan = YES;
    self.saiXuanView.hidden = YES;
    self.zheDangView.hidden = YES;
}
- (IBAction)doingSaiXuanOrder:(UIButton *)sender {
    
    if (self.isShaiXuan == YES) {
        self.isShaiXuan = NO;
        self.xiaLaImageView.image = [UIImage imageNamed:@"Whole"];
        self.saiXuanView.hidden = NO;
        self.zheDangView.hidden = NO;
    }else{
        self.isShaiXuan = YES;
        self.xiaLaImageView.image = [UIImage imageNamed:@"Whole1"];
        self.saiXuanView.hidden = YES;
        self.zheDangView.hidden = YES;
    }
    
}

- (IBAction)showAllOrdr:(UIButton *)sender {
    self.quanbuLb.textColor = [UIColor orangeColor];
    self.qiangxiuLb.textColor = [UIColor darkGrayColor];
    self.yuYueLb.textColor = [UIColor darkGrayColor];
    self.isShaiXuan = YES;
    self.saiXuanView.hidden = YES;
    self.zheDangView.hidden = YES;
    self.xiaLaLb.text = @"全部";
    self.xiaLaImageView.image = [UIImage imageNamed:@"Whole1"];
}
- (IBAction)showQiangXiuOrder:(UIButton *)sender {
    self.quanbuLb.textColor = [UIColor darkGrayColor];
    self.qiangxiuLb.textColor = [UIColor orangeColor];
    self.yuYueLb.textColor = [UIColor darkGrayColor];
    self.isShaiXuan = YES;
    self.saiXuanView.hidden = YES;
    self.zheDangView.hidden = YES;
    self.xiaLaLb.text = @"抢修单";
    self.xiaLaImageView.image = [UIImage imageNamed:@"Whole1"];
}
- (IBAction)showYuyueOrder:(UIButton *)sender {
    self.quanbuLb.textColor = [UIColor darkGrayColor];
    self.qiangxiuLb.textColor = [UIColor darkGrayColor];
    self.yuYueLb.textColor = [UIColor orangeColor];
    self.isShaiXuan = YES;
    self.saiXuanView.hidden = YES;
    self.zheDangView.hidden = YES;
    self.xiaLaLb.text = @"预约单";
    self.xiaLaImageView.image = [UIImage imageNamed:@"Whole1"];
}



@end
